<script setup>

import { onMounted, ref } from 'vue'
import {useRoute} from 'vue-router'
import { getDetailAPI } from '@/apis/detail.js'
import { useCartStore } from '@/stores/cartStore';
import { useUserStore } from '@/stores/user';
import router from '@/router';

const route = useRoute()
const cartStore = useCartStore()
const userStore = useUserStore()
const detailInfo = ref({})
const imageList = ref([])

const getDetailInfo = async (id) => {
    const res = await getDetailAPI(id)
    detailInfo.value = res.result
    imageList.value.push(detailInfo.value.image)
    console.log(detailInfo.value)

}

onMounted(() => {
  getDetailInfo(route.params.id)

})

const count = ref(1)
const countChange = () => {
  console.log(count)
}
// 加入购物车
const addCart = () => {
  if(detailInfo.value.id){
    // 加入购物车
    cartStore.addCart({
      id: detailInfo.value.id,
      title: detailInfo.value.title,
      image: detailInfo.value.image,
      price: detailInfo.value.price,
      count: count.value,
      type: detailInfo.value.type,
      press: detailInfo.value.press,
      selected: true
    }).then((res)=>{
      if(res.code === 200)
        ElMessage.success('加入购物车成功');
      else{
        ElMessage.info({
          message: '请先登录账号！',
          duration: 1000
        });

        userStore.toLoginPage(route.path)
        setTimeout(() => {
         router.push({ path: '/login' })
        }, 1000); // 延迟1秒
      }

    }).catch(()=>{
    })
  }else{
    ElMessage.warning('请选择商品规格'); // 提示用户选择规格

  }
}

</script>

<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="detailInfo.title">
      <div class="bread-container" >
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ detailInfo.title }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 商品信息 -->
      <div class="info-container">
        <div>
          <div class="goods-info">
            <div class="media" >
              <!-- 图片预览区 -->
              <el-image
               class="img"
               :preview-src-list="imageList"
               :src="detailInfo.image"
               alt=""
               fit="contain"/>
            </div>
            <div class="spec">
              <!-- 商品信息区 -->
              <p class="g-name"> {{ detailInfo.title }} </p>
              <p class="g-price">
                <span>{{ detailInfo.price }}</span>
                <span> {{ detailInfo.oldPrice }}</span>
              </p>
              <p class="g-desc" v-html="detailInfo.info"></p>
              <div class="g-service">
                <dl>
                  <dt>作者</dt>
                  <dd>{{ detailInfo.editor }}</dd>
                </dl>
                <dl>
                  <dt>发行日期</dt>
                  <dd>{{ detailInfo.date }}</dd>
                </dl>
                <dl>
                  <dt>出版社</dt>
                  <dd>{{ detailInfo.press }}</dd>
                </dl>
                <dl>
                  <dt>促销</dt>
                  <dd>12月好物放送，App领券购买直降120元</dd>
                </dl>
                <dl>
                  <dt>服务</dt>
                  <dd>
                    <span>无忧退货</span>
                    <span>快速退款</span>
                    <span v-if=" Number(detailInfo.price) < 68">满68元包邮</span>
                    <span v-else>免费包邮</span>
                    <a href="javascript:;">了解详情</a>
                  </dd>
                </dl>
              </div>
              <!-- 数据组件 -->
              <el-input-number v-model="count" :min="1" class="my-counter"  @click="countChange"></el-input-number>
              <!-- 按钮组件 -->
              <div>
                <el-button size="large" class="btn" @click="addCart">
                  加入购物车
                </el-button>
              </div>

            </div>
          </div>
          <div class="goods-footer">
            <div class="goods-article">
              <!-- 商品详情 -->
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<style scoped lang='scss'>
.xtx-goods-page {
  .goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;

    .media {
      width: 580px;
      height: 600px;
      padding: 30px 50px;

      .img {
        width: 450px;
        height: 450px;

        &:hover {
          border:1px solid #2aba9b;
        }
      }

    }

    .spec {
      flex: 1;
      padding: 30px 30px 30px 0;
    }
  }

  .goods-footer {
    display: flex;
    margin-top: 20px;

    .goods-article {
      width: 940px;
      margin-right: 20px;
    }

    .goods-aside {
      width: 280px;
      min-height: 1000px;
    }
  }

  .goods-tabs {
    min-height: 600px;
    background: #fff;
  }

  .goods-warn {
    min-height: 600px;
    background: #fff;
    margin-top: 20px;
  }

  .number-box {
    display: flex;
    align-items: center;

    .label {
      width: 60px;
      color: #999;
      padding-left: 10px;
    }
  }

  .g-name {
    font-size: 22px;
  }

  .g-desc {
    color: #999;
    margin-top: 10px;
  }

  .g-price {
    margin-top: 10px;

    span {
      &::before {
        content: "¥";
        font-size: 14px;
      }

      &:first-child {
        color: $priceColor;
        margin-right: 10px;
        font-size: 22px;
      }

      &:last-child {
        color: #999;
        text-decoration: line-through;
        font-size: 16px;
      }
    }
  }

  .g-service {
    background: #f5f5f5;
    width: 500px;
    padding: 20px 10px 0 10px;
    margin-top: 10px;

    dl {
      padding-bottom: 20px;
      display: flex;
      align-items: center;

      dt {
        width: 65px;
        color: #999;
      }

      dd {
        color: #666;

        &:last-child {
          span {
            margin-right: 10px;

            &::before {
              content: "•";
              color: $xtxColor;
              margin-right: 2px;
            }
          }

          a {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .goods-sales {
    display: flex;
    width: 400px;
    align-items: center;
    text-align: center;
    height: 140px;

    li {
      flex: 1;
      position: relative;

      ~li::after {
        position: absolute;
        top: 10px;
        left: 0;
        height: 60px;
        border-left: 1px solid #e4e4e4;
        content: "";
      }

      p {
        &:first-child {
          color: #999;
        }

        &:nth-child(2) {
          color: $priceColor;
          margin-top: 10px;
        }

        &:last-child {
          color: #666;
          margin-top: 10px;

          i {
            color: $xtxColor;
            font-size: 14px;
            margin-right: 2px;
          }

          &:hover {
            color: $xtxColor;
            cursor: pointer;
          }
        }
      }
    }
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      >span {
        color: $priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
    }
  }
}

.goods-detail {
  padding: 40px;

  .attrs {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;

    li {
      display: flex;
      margin-bottom: 10px;
      width: 50%;

      .dt {
        width: 100px;
        color: #999;
      }

      .dd {
        flex: 1;
        color: #666;
      }
    }
  }

  >img {
    width: 100%;
  }
}

.btn {
  margin-top: 20px;

}

.bread-container {
  padding: 25px 0;
}

.my-counter{
  margin-top:20px;
}
</style>
